<?

/** mysql Class **/

class mysql {
	
	private $link;
	private $query;
	private $sql;
	private $error;
	public function __construct($link=null){
		if($link != null and $link == true)
			$this->link = $link;
	}
	
	public function __destruct(){
		$this->Disconnect($this->link);
	}
	
	public function Connect($host,$user,$pass){
		$this->link = @mysql_connect($host,$user,$pass) or die('MySQL Connect Error :'.__FILE__.' Line:'.__LINE__);
		//register_shutdown_function('mysql_close',$this->link);
		return $this->link;
	}
	
	public function Disconnect($link=null){
		if($link == null)
			@mysql_close();
		else
			@mysql_close($link);
	}
	
	public function SetCharset($charset){
		if(!$this->link)
			die('MySQL Connect Error :'.__FILE__.' Line:'.__LINE__);
			
		@mysql_set_charset($charset,$this->link);
	}
	
	public function SetDB($db){
		if(!$this->link)
			die('MySQL Connect Error :'.__FILE__.' Line:'.__LINE__);
			
		return @mysql_query("use ".$db,$this->link) or die('MySQL Select DB Error :'.__FILE__.' Line:'.__LINE__);
	}
	
	public function Info(){
		return mysql_info($this->link);
	}
	
	public function Query($sql){
		if(!$this->link)
			die('MySQL Connect Error :'.__FILE__.' Line:'.__LINE__);
			
		$this->sql = $sql;
		$this->query = @mysql_query($this->sql,$this->link);
		if(!$this->query) $this->error = '['.mysql_errno().'] '.mysql_error();
		return $this->query;
	}
	
	public function SQL(){
		return $this->sql;
	}
	public function AntiHack($text,$m=true){
		if($m == true)
			return mysql_real_escape_string(htmlspecialchars($text));
		else
			return mysql_real_escape_string($text);
	}
	
	public function Insert($table,$valueAR,$options=null){
	
		$sql = 'insert into `'.$table.'` ';
		$sqlField = null;
		$sqlValue = null;
		
		foreach($valueAR as $field => $value){
			if($sqlField != null) $sqlField .=',';
			if($sqlValue != null) $sqlValue .=',';
			if(empty($value)) $sqlValue .= 'NULL'; else 
			{
				switch($value){
						case "NOW()" : $sqlValue .= 'NOW()'; break;
						default : $sqlValue .= "'".$this->AntiHack($value)."'"; break;
				}
			}
			$sqlField .= $field;
		}
		$sql .='('.$sqlField.') ';

		$sql .=' values('.$sqlValue.')';
		
		if($option != null) $sql .=' '.$option;
		$ret=$this->Query($sql);
		if($ret)
			@mysql_insert_id($ret);
		else
			return $ret;
		
	}
	
	public function Update($table,$valueAR,$option=null){
				
			
			$sql = 'update `'.$table.'` set ';
			$sqlValue = null;
			foreach($valueAR as $field => $value){
				if($sqlValue != null) $sqlValue .=',';		
				
				if(empty($value)) $sqlValue .= $field."=NULL"; else 
				{
					switch($value){
							case "NOW()" : $sqlValue .= $field."=".'NOW()'; break;
							default : $sqlValue .= $field."='".$this->AntiHack($value)."'"; break;
					}
				}
			}
			$sql .=$sqlValue;
			if($option != null) $sql .=' '.$option;
			$ret =$this->Query($sql);
			return $ret;
			
	}
	
	public function FObject(){
		if(!$this->query) die($this->error);
		
		return mysql_fetch_object($this->query);

	}
	
	public function FArray(){
		if(!$this->query) die($this->error);

		return mysql_fetch_array($this->query);

	}
	
	public function Num(){
		if(!$this->query) die($this->error);
		return mysql_num_rows($this->query);
	}
        
        public function Sequence($up_tb,$seq_name){
            $sql = "UPDATE ".$up_tb." SET seq_val = seq_val +1 WHERE seq_name ='".$seq_name."'";
            $this->Query($sql);
            
            $sql = "SELECT seq_val FROM sequence WHERE seq_name ='".$seq_name."'";
            $this->Query($sql);
            $ret = $this->FObject();
            $next_val = $ret->seq_val;
            
            return $next_val;
        }
}
?>